package com.spsoft.vatoutinv.service.flowprocess;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.exception.BusinessException;
import com.spsoft.core.model.ProcessParamBo;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.vatoutinv.constants.CommonConstant;
import com.spsoft.vatoutinv.entity.vmshzqrdmanage.VmsApprovalHzqrd;
import com.spsoft.vatoutinv.enums.CommonEnum;
import com.spsoft.vatoutinv.service.vmshzqrdmanage.VmsApprovalHzqrdService;
import com.spsoft.vatoutinv.service.vmshzqrdmanage.VmsHzqrdConfirmService;
import com.spsoft.workflow.common.enums.bus.HzqrdConfirmEnums;
import com.spsoft.workflow.service.BusinessHandlerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 数电红字确认单确认流程处理业务实现类
 *
 * @author : chenxudong
 * @date : 2024/1/15 9:25
 */
@Service("FLOW_HZQRDQR")
@Slf4j
@RequiredArgsConstructor
public class HzqrdConfirmProcessHandlerServiceImpl implements BusinessHandlerService {

    private final VmsHzqrdConfirmService vmsHzqrdConfirmService;

    private final VmsApprovalHzqrdService vmsApprovalHzqrdService;

    @Override
    public void processHandler(ProcessParamBo pp) throws BusinessException {
        Long busId = pp.getBusId();
        String busDataJson = pp.getBusData();
        String nodeId = pp.getNodeId();
        HzqrdConfirmEnums e = HzqrdConfirmEnums.valueOfCode(nodeId);
        if (e == null) {
            throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "审批流程有误，请联系管理员");
        }
        switch (e) {
            case ROOT:
                /**
                 * 提交节点，判断如下
                 * 1.是否选择了红字确认单确认结果
                 * 2.红字确认单确认数据是否保存成功
                 */
                if (StringUtil.isBlank(busDataJson)) {
                    throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "红字信息确认单确认结果不能为空");
                }
                JSONObject jsonObject = JSONObject.parseObject(busDataJson);
                String hzqrdqrjg = jsonObject.getString("hzqrdqrjg");
                if (!StrUtil.equalsAny(hzqrdqrjg, CommonConstant.STR_Y, CommonConstant.STR_N)) {
                    throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "红字信息确认单确认结果只能为同意或不同意");
                }
                LambdaQueryWrapper<VmsApprovalHzqrd> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(VmsApprovalHzqrd::getApplyId, busId);
                VmsApprovalHzqrd approvalHzqrd = vmsApprovalHzqrdService.getOne(wrapper);
                if (approvalHzqrd == null) {
                    throw new BusinessException(ResponseEnum.HZQRD_NOT_FIND_ERROR);
                }
                break;
            case FLOW_HZQRDQR_SP:
                /**
                 * 审批节点，调用乐企接口进行数电红字确认单确认
                 */
                if (StringUtil.isBlank(busDataJson)) {
                    throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "红字信息确认单确认结果不能为空");
                }
                JSONObject jsonObject2 = JSONObject.parseObject(busDataJson);
                String qrjg = jsonObject2.getString("hzqrdqrjg");
                if (!StrUtil.equalsAny(qrjg, CommonConstant.STR_Y, CommonConstant.STR_N)) {
                    throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "红字信息确认单确认结果只能为同意或不同意");
                }
                Result<Object> result = vmsHzqrdConfirmService.hzqrdConfirm(busId, qrjg);
                if (!ResponseEnum.SUCCESS.getCode().equals(result.getCode())) {
                    throw new BusinessException(result.getCode(), result.getMessage());
                }
                break;
            default:
                // 未知节点报错
                throw new BusinessException(ResponseEnum.PROCESS_NODE_ERROR);
        }
    }

    @Override
    public boolean stop(Long busId) {
        // 终止流程时，需要将红字确认单申请数据删除
        LambdaQueryWrapper<VmsApprovalHzqrd> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VmsApprovalHzqrd::getApplyId, busId);
        VmsApprovalHzqrd approvalHzqrd = vmsApprovalHzqrdService.getOne(wrapper);
        if (approvalHzqrd != null) {
            // 删除数据
            LambdaUpdateWrapper<VmsApprovalHzqrd> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(VmsApprovalHzqrd::getYxbz, CommonEnum.HZQRD_YXBZ_NO.getKey());
            updateWrapper.set(VmsApprovalHzqrd::getDelFlag, CommonEnum.DEL_STATUS_1.getKey());
            updateWrapper.eq(VmsApprovalHzqrd::getId, approvalHzqrd.getId());
            vmsApprovalHzqrdService.update(updateWrapper);
        }
        return true;
    }
}
